/*
 * Copyright (C) 2012-2013  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.resources.impl.internal;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.ResourceBundle.Control;

import nemos.resources.impl.IControl;

/**
 * ControlAdapter : The class adapting an {@link IControl} into a
 * {@link Control}.
 * 
 * @author Romain DUBOIS
 */
public class ControlAdapter extends Control {

    /** The adapted control */
    private final IControl mControl;
    /** The handled resources type */
    private final List<String> mType;

    /**
     * Constructor
     * 
     * @param pControl
     *            The control to adapt
     * @param pType
     *            The handled resources type
     */
    public ControlAdapter(final IControl pControl, final Class<?> pType) {
        super();

        if (pControl == null) {
            throw new NullPointerException();
        }

        mControl = pControl;
        mType = Collections.singletonList(pType.getName());
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.util.ResourceBundle.Control#getFormats(java.lang.String)
     */
    @Override
    public List<String> getFormats(final String pBaseName) {
        if (pBaseName == null) {
            throw new NullPointerException();
        }
        return mType;
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.util.ResourceBundle.Control#getTimeToLive(java.lang.String,
     *      java.util.Locale)
     */
    @Override
    public long getTimeToLive(final String pBaseName, final Locale pLocale) {
        return mControl.getTimeToLive(pBaseName, pLocale);
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.util.ResourceBundle.Control#needsReload(java.lang.String,
     *      java.util.Locale, java.lang.String, java.lang.ClassLoader,
     *      java.util.ResourceBundle, long)
     */
    @Override
    public boolean needsReload(final String pBaseName, final Locale pLocale, final String pFormat,
            final ClassLoader pLoader, final ResourceBundle pBundle, final long pLoadTime) {
        return mControl.needsReload(pBaseName, pLocale, pFormat, pLoader, pBundle, pLoadTime);
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.util.ResourceBundle.Control#newBundle(java.lang.String,
     *      java.util.Locale, java.lang.String, java.lang.ClassLoader, boolean)
     */
    @Override
    public ResourceBundle newBundle(final String pBaseName, final Locale pLocale,
            final String pFormat, final ClassLoader pLoader, final boolean pReload)
            throws IllegalAccessException, InstantiationException, IOException {
        return mControl.newBundle(pBaseName, pLocale, pFormat, pLoader, pReload);
    }
}
